package dao;

import entity.Review;
import utils.JDBCUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class ReviewDao {

    // 插入评价数据
    public boolean insertReview(List<Review> reviews,int userId) {
        String sql = "INSERT INTO reviews (product_id, USER_ID,review) VALUES (?, ?, ?)";
        boolean isSuccess = true;

        try (Connection connection = JDBCUtil.getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql)) {

            // 遍历所有的评论数据
            for (Review review : reviews) {
                preparedStatement.setInt(1, review.getProductId());
                preparedStatement.setInt(2, userId);
                preparedStatement.setString(3, review.getReviewText());

                // 执行插入
                int rowsAffected = preparedStatement.executeUpdate();
                if (rowsAffected == 0) {
                    isSuccess = false;  // 如果没有数据被插入，标记为失败
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
            isSuccess = false;  // 如果发生异常，标记为失败
        }
        return isSuccess;
    }

    public List<Review> queryAll() {
        List<Review> reviews = new ArrayList<>();

        String sql = "SELECT REVIEW_ID, PRODUCT_ID, USER_ID, REVIEW, REVIEW_DATE FROM reviews";

        try (Connection connection = JDBCUtil.getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
            ResultSet resultSet = preparedStatement.executeQuery();

            while (resultSet.next()) {
                Review review = new Review();
                review.setReviewId(resultSet.getInt("REVIEW_ID"));
                review.setProductId(resultSet.getInt("PRODUCT_ID"));
                review.setUserId(resultSet.getInt("USER_ID"));
                review.setReviewText(resultSet.getString("REVIEW"));
                review.setReviewDate(resultSet.getTimestamp("REVIEW_DATE"));

                reviews.add(review);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return reviews;
    }
    public List<Review> queryReviewsByProductId(int productId) {
        List<Review> reviews = new ArrayList<>();

        String sql = "SELECT REVIEW_ID, PRODUCT_ID, USER_ID, REVIEW, REVIEW_DATE FROM reviews WHERE PRODUCT_ID = ?";

        try (Connection connection = JDBCUtil.getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
            preparedStatement.setInt(1, productId);
            ResultSet resultSet = preparedStatement.executeQuery();

            while (resultSet.next()) {
                Review review = new Review();
                review.setReviewId(resultSet.getInt("REVIEW_ID"));
                review.setProductId(resultSet.getInt("PRODUCT_ID"));
                review.setUserId(resultSet.getInt("USER_ID"));
                review.setReviewText(resultSet.getString("REVIEW"));
                review.setReviewDate(resultSet.getTimestamp("REVIEW_DATE"));
                reviews.add(review);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return reviews;
    }
}
